package com.cuijq.aclservice.controller;


import com.cuijq.aclservice.entity.Permission;
import com.cuijq.aclservice.service.PermissionService;
import com.cuijq.eduschool.commontuils.R;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-17
 */
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {
  @Autowired
  private PermissionService permissionService;

  //获取全部菜单
  @ApiOperation(value = "查询所有菜单")
  @GetMapping
  public R indexAllPermission() {
    List<Permission> list =  permissionService.queryAllMenuGuli();
    return R.ok().data("children",list);
  }

  @ApiOperation(value = "递归删除菜单")
  @DeleteMapping("remove/{id}")
  public R remove(@PathVariable String id) {
    permissionService.removeChildByIdGuli(id);
    return R.ok();
  }

  @ApiOperation(value = "给角色分配权限")
  @PostMapping("/doAssign")
  public R doAssign(String roleId,String[] permissionId) {
    permissionService.saveRolePermissionRealtionShipGuli(roleId,permissionId);
    return R.ok();
  }

  @ApiOperation(value = "根据角色获取菜单")
  @GetMapping("toAssign/{roleId}")
  public R toAssign(@PathVariable String roleId) {
    List<Permission> list = permissionService.selectAllMenu(roleId);
    return R.ok().data("children", list);
  }



  @ApiOperation(value = "新增菜单")
  @PostMapping("save")
  public R save(@RequestBody Permission permission) {
    permissionService.save(permission);
    return R.ok();
  }

  @ApiOperation(value = "修改菜单")
  @PutMapping("update")
  public R updateById(@RequestBody Permission permission) {
    permissionService.updateById(permission);
    return R.ok();
  }
}

